package com.example.service.impl;

import java.math.BigDecimal;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.common.HttpStatusConstants;
import com.example.common.R;
import com.example.mapper.AppointmentMapper;
import com.example.mapper.RebackMapper;
import com.example.mapper.UserMapper;
import com.example.model.AddAppointmentDTO;
import com.example.model.req.AddOrderReq;
import com.example.service.AppointmentService;
import com.example.utils.JwtUtil;

@Service
public class AppointmentServiceImpl implements AppointmentService {
    @Autowired
    private AppointmentMapper appointmentMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RebackMapper rebackMapper;

    @Override
    public R<String> addAppointment(AddOrderReq addOrderReq, HttpServletRequest request) {
        try {
            String openId = JwtUtil.getOpenIdFromToken(request.getHeader("Authorization").replace("Bearer ", ""));
            Integer userId = userMapper.getIdByOpenId(openId);

            AddAppointmentDTO addAppointmentDTO = AddAppointmentDTO.builder()
                    .applicantId(userId)
                    .approverId(addOrderReq.getWorker())
                    .appointmentTime(addOrderReq.getStartTime())
                    .address(addOrderReq.getAddress())
                    .build();
            appointmentMapper.addAppointment(addAppointmentDTO);

            BigDecimal balance = userMapper.getBalanceById(userId);
            BigDecimal price = userMapper.getPriceById(addAppointmentDTO.getApproverId());
            BigDecimal result = balance.subtract(price);
            if (result.signum() == -1) {
                return R.error(HttpStatusConstants.INTERNAL_SERVER_ERROR, "余额不足，请充值");
            }
            userMapper.setBalanceById(userId, result);

            return R.success("预约成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(HttpStatusConstants.INTERNAL_SERVER_ERROR, "请稍后重试");
        }
    }

    @Override
    public R<String> appointmentReback(Integer appointmentId, HttpServletRequest request) {
        try {
            String openId = JwtUtil.getOpenIdFromToken(request.getHeader("Authorization").replace("Bearer ", ""));
            Integer userId = userMapper.getIdByOpenId(openId);

            rebackMapper.addAppointmentReback(appointmentId, userId);
            appointmentMapper.updateReback(userId);
            return R.success();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(HttpStatusConstants.INTERNAL_SERVER_ERROR, "");
        }
    }

    @Override
    public R<String> completeAppointment(Integer appointmentId) {
        try {
            appointmentMapper.completeAppointment(appointmentId);
            rebackMapper.addAppointmentReback(appointmentId, appointmentId);
            appointmentMapper.updateReback(appointmentId);
            return R.success();
        } catch (Exception e) {
            return R.error(HttpStatusConstants.INTERNAL_SERVER_ERROR, "");
        }
    }

    @Override
    public R<String> progressAppointment(Integer appointmentId) {
        appointmentMapper.progressAppointment(appointmentId);
        return R.success();
    }
}
